﻿///************* Mediatheque.cs ***************///
/// auteur: Amor REBHI                         ///
/// Principe: gere l'ensemble de la mediateque ///
/// *******************************************///



using MyVideoManager.DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

using System.IO; //Pour le stream
using System.Xml.Serialization; //Pour la serialization
using System.Runtime.Serialization;
using Windows.Storage;
using Windows.Storage.Streams;

namespace MyVideoManager.DataModel
{
    class Mediatheque
    {
        public List<Dossier> lstDossier { get; set; }
        string fichier = @"data.xml";

        public Mediatheque()
        {
            this.lstDossier = new List<Dossier>();
        }

        public void addDossier(Dossier monDossier)
        {
            lstDossier.Add(monDossier);
        }

        //todo: faire constructeur


        //faire methode de serialisation
        public async void serialisation()
        {
            StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(fichier, CreationCollisionOption.ReplaceExisting);
            IRandomAccessStream sessionRandomAccess = await sessionFile.OpenAsync(FileAccessMode.ReadWrite);
            IOutputStream sessionOutputStream = sessionRandomAccess.GetOutputStreamAt(0);
            var sessionSerializer = new DataContractSerializer(typeof(List<Dossier>), new Type[] { typeof(Dossier) });
            sessionSerializer.WriteObject(sessionOutputStream.AsStreamForWrite(), lstDossier);
            await sessionOutputStream.FlushAsync();

        }

        public async void deserialisation()
        {

            StorageFile sessionFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(fichier, CreationCollisionOption.OpenIfExists);
            if (sessionFile == null)
            {
                return;
            }
            IInputStream sessionInputStream = await sessionFile.OpenReadAsync();
            var sessionSerializer = new DataContractSerializer(typeof(List<Dossier>), new Type[] { typeof(Dossier) });

            if (sessionInputStream.AsStreamForRead().Length > 0)
            {
                lstDossier = (List<Dossier>)sessionSerializer.ReadObject(sessionInputStream.AsStreamForRead());
            }

            

        }

           
    }
}
